var Map=cc.Layer.extend({
	tilemap:null,
	player:null,
	
	tileSize:null,
	mapSize:null,

	bottomline:16,
	stateObjs:null,
	removeList:null,
	barrierRects:null,

	lastDt:0,
	dataid:0,
	init:function(dataid){
		this.tilemap=cc.TMXTiledMap.create(s_tmxtest);
		this.tileSize=this.tilemap.getTileSize();
		this.mapSize=this.tilemap.getContentSize();

		this.addChild(this.tilemap,0,1);
		this.stateObjs=[];

		this.scheduleUpdate();

		this.dataid=dataid;
		this.initBarrierRects();
		
	},
	//把障碍物整合成矩形列表，方便进行碰撞判断
	initBarrierRects:function()
	{
		this.barrierRects=[];
		var baRects=MapsConfig[this.dataid];
		for (var i = 0; i < baRects.barriers.length; i++) {
			var rti=baRects.barriers[i];
			
			this.barrierRects.push(cc.rect(rti[0],rti[1],rti[2],rti[3]));

			var uR = cc.LayerColor.create(cc.c4b(0, 0, 255, 180));
            this.addChild(uR);
            uR.setAnchorPoint(0,0);
            uR.setPosition(rti[0],rti[1]);
        	uR.setContentSize(rti[2],rti[3]);
		};
	},
	addStateObj:function(stateobj){
		this.stateObjs.push(stateobj);
	},
	removeStateObj:function(stateobj)
	{
		if(this.removeList==null)
			this.removeList=[];

		this.removeList.push(stateobj);

		var iRemove=-1;
		for (var i = 0; i < this.stateObjs.length; i++) {
			if(this.stateObjs[i]==stateobj)
				iRemove=i;
		};
		this.stateObjs.splice(iRemove,1);
	},
	getStateObjs:function(){
		return this.stateObjs;
	},
	center:function(x,y){
		var director = cc.Director.getInstance();
		var winsize= director.getWinSize();

		var mappx=this.getPositionX();
		var mappy=this.getPositionY();


		var delx=x-winsize.width/2;
		var dely=y-winsize.height/2;

		this.setPosition(-delx,mappy)//地图y方向不移动
	},
	setZoomPlayer:function(player){
		this.player=player;
	},
	update:function(dt){
		this._super();

		for(var i=0;i<this.stateObjs.length;++i)
		{
			this.stateObjs[i].updateState(dt);
		}

		this.center(this.player.getPositionX(),this.player.getPositionY())

		if(this.removeList!=null)
		{
			for (var i = 0; i < this.removeList.length; i++) {
				var stateobj=this.removeList.pop();
				this.removeChild(stateobj);
			};
		}
	},
	
	placePassable:function(ipos){
		var passable=0;

		//物体是以左下角为原点，地图格子以左上角为原点
		var brick=this.changePixelToTile(ipos.x,ipos.y);

		if(brick==null)
		{
			cc.log("brick==null")
			return 2;
		}

		var maplayer=this.tilemap.getLayer("barrier");
		var gid=maplayer.getTileGIDAt(brick);

		//如果没有阻挡，要看地图设置的最低位置。
		var bEmpty=gid==0;
		if(bEmpty==true)
		{
			if(ipos.y<=this.bottomline)
			{
				passable=2;
			}
		}else
		{
			passable=1;
		}

		return passable;
	},

	//0可以通过，1不可以，2超出地图,传入世界坐标
	passableByRect:function(inrect){

		//障碍物判断
		for (var i = 0; i < this.barrierRects.length; i++) {
			//var transRt=CmnUtils.rectToWorldRect(this,this.barrierRects[i]);
			if(cc.rectIntersectsRect(this.barrierRects[i],inrect))
				return 1;
		};

		var maprect=cc.rect(0,0,this.mapSize.width,this.mapSize.height);
		if(cc.rectContainsRect(maprect,inrect)==false)
			return 2;
		

		return 0;
	},
	changePixelToTile:function(ix,iy){//传入左下角为原点，返回左上角格子坐标

		if(iy<=0||iy>=this.mapSize.height-this.bottomline
			||ix<=0||ix>=this.mapSize.width)
		{
			return null;
		}


		var x=Math.floor(ix/this.tileSize.width);
		var y=Math.floor((this.mapSize.height-iy)/this.tileSize.height);

		return cc.p(x,y);
	},
	getXPosPixel:function(x,dir){//获取砖块像素坐标,dir=1向右走，dir=-1向左走
		var posx=(x-dir)*this.tileSize.width;
		return posx;
	},
	getYPosPixel:function(y,dir){//获取砖块像素坐标,dir=1向下走，dir=-1向上走
		var posy=(y-dir)*this.tileSize.height;
		return posy;
	}
})